package com.eduplatform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduplatform.common.Result;
import com.eduplatform.common.UserContext;
import com.eduplatform.entity.Course;
import com.eduplatform.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/courses")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserContext userContext;

    @GetMapping("/list")
    public Result<Page<Course>> listCourses(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "subject", required = false) Long subject,
            @RequestParam(value = "level", required = false) Integer level,
            @RequestParam(value = "type", required = false) String type) {

        // 调用带过滤条件的方法
        Page<Course> coursePage = courseService.listCoursesWithFilters(page, size, keyword, subject, level, type);
        return Result.success(coursePage);
    }

    @GetMapping("/recommend")
    public Result<Page<Course>> listCourses(
            @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        Page<Course> coursePage = courseService.listCourses(1, limit);
        return Result.success(coursePage);
    }

    @GetMapping("/teacher/{teacherId}")
    public Result<Page<Course>> listCoursesByTeacher(
            @PathVariable Long teacherId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<Course> coursePage = courseService.listCoursesByTeacher(teacherId, page, size);
        return Result.success(coursePage);
    }

    @GetMapping("/{id}")
    public Result<Course> getCourseById(@PathVariable Long id) {
        Course course = courseService.getById(id);
        if (course != null) {
            return Result.success(course);
        } else {
            return Result.error("Course not found");
        }
    }

    @PostMapping("/create")
    public Result<Boolean> createCourse(@RequestBody @Valid Course course) {
        Long currentUserId = userContext.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        course.setTeacherId(currentUserId);
        boolean result = courseService.save(course);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("Failed to create course");
        }
    }

    @PutMapping("/update")
    public Result<Boolean> updateCourse(@RequestBody @Valid Course course) {
        boolean result = courseService.updateById(course);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("Failed to update course");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteCourse(@PathVariable Long id) {
        boolean result = courseService.removeById(id);
        if (result) {
            return Result.success(true);
        } else {
            return Result.error("Failed to delete course");
        }
    }
}